package com.creditharmony.car.loan.controller;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.creditharmony.car.common.base.bean.out.BaseDataOutBean;
import com.creditharmony.car.common.base.bean.out.BaseOutBean;
import com.creditharmony.car.common.base.controller.GatewayBaseController;
import com.creditharmony.car.common.common.vo.requestvo.CarLoanCustomerRequestVo;
import com.creditharmony.car.common.common.vo.responsevo.CarLoanCustomerResponseVo;
import com.creditharmony.car.common.exception.UserException;
import com.creditharmony.car.common.utils.ControllerUtils;
import com.creditharmony.car.common.utils.ErrorType;
import com.creditharmony.car.common.utils.MicroServiceLogger;
import com.creditharmony.car.loan.service.CarLoanCustomerService;

/**
 * 客户个人信息处理.
 */
@RestController
@RequestMapping("/customer")
public class CarLoanCustomerController extends GatewayBaseController {
    /** 日志 */
    private static final Logger logger = LoggerFactory.getLogger(CarLoanCustomerController.class);
    /** 日志采集. */
    @Autowired
    private MicroServiceLogger microServiceLogger;
    /** 客户个人信息服务. */
    @Autowired
    private CarLoanCustomerService carLoanCustomerService;

    /**
     * 客户个人信息录入.
     * @param requestVo 请求信息
     * @param request 请求对象
     * @return 处理结果
     */
    @CrossOrigin
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseOutBean add(@RequestBody CarLoanCustomerRequestVo requestVo, HttpServletRequest request) {
        /** 处理名称. */
        String procName = "客户个人信息录入";
        
        // 初始化请求数据
        ControllerUtils.initBaseInfo(requestVo, serviceIp, request);
        // 日志:请求
        microServiceLogger.doInLogger(logger, requestVo, procName);
        BaseOutBean outBean = null;
        try {
            carLoanCustomerService.insertCarCustomer(requestVo);
            outBean = new BaseOutBean(ErrorType.NONE, requestVo.getSerialNum());
        } catch (Exception e) {
            logger.info(e.getMessage());
            if (e instanceof UserException) {
                outBean = new BaseOutBean(ErrorType.FAIL_BUSSINESS, requestVo.getSerialNum());
            } else {
                outBean = new BaseOutBean(ErrorType.ERROR_SYS_DB, requestVo.getSerialNum());
            }
            // 日志:异常
            microServiceLogger.doErrorLogger(logger, outBean, procName, e.getMessage(), e);
        }
        // 日志:返回
        microServiceLogger.doOutLogger(logger, outBean, procName);
        return outBean;
    }
    
    /**
     * 客户个人信息查询.
     * @param requestVo 请求信息
     * @param request 请求对象
     * @return 处理结果
     */
    @CrossOrigin
    @RequestMapping(value = "/getByLoanCode", method = RequestMethod.POST)
    public BaseDataOutBean<CarLoanCustomerResponseVo> getByLoanCode(@RequestBody CarLoanCustomerRequestVo requestVo, HttpServletRequest request) {
        /** 处理名称. */
        String procName = "客户个人信息查询";
        // 初始化请求数据
        ControllerUtils.initBaseInfo(requestVo, serviceIp, request);
        // 日志:请求
        microServiceLogger.doInLogger(logger, requestVo, procName);
        BaseDataOutBean<CarLoanCustomerResponseVo> outBean = null;
        try {
            CarLoanCustomerResponseVo outVo = carLoanCustomerService.selectByLoanCode(requestVo.getLoanCode());
            outBean = new BaseDataOutBean<CarLoanCustomerResponseVo>(ErrorType.NONE, requestVo.getSerialNum());
            outBean.setData(outVo);
        } catch (Exception e) {
            logger.info(e.getMessage());
            if (e instanceof UserException) {
                outBean = new BaseDataOutBean<CarLoanCustomerResponseVo>(ErrorType.FAIL_BUSSINESS, requestVo.getSerialNum());
            } else {
                outBean = new BaseDataOutBean<CarLoanCustomerResponseVo>(ErrorType.ERROR_SYS, requestVo.getSerialNum());
            }
            // 日志:异常
            microServiceLogger.doErrorLogger(logger, outBean, procName, e.getMessage(), e);
        }
        // 日志:返回
        microServiceLogger.doOutLogger(logger, outBean, procName);
        return outBean;
    }
    
    /**
     * 客户个人信息更新.
     * @param requestVo 请求信息
     * @param request 请求对象
     * @return 处理结果
     */
    @CrossOrigin
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseOutBean update(@RequestBody CarLoanCustomerRequestVo requestVo, HttpServletRequest request) {
        /** 处理名称. */
        String procName = "客户个人信息更新";
        // 初始化请求数据
        ControllerUtils.initBaseInfo(requestVo, serviceIp, request);
        // 日志:请求
        microServiceLogger.doInLogger(logger, requestVo, procName);
        BaseOutBean outBean = null;
        try {
            carLoanCustomerService.updateCarLoanCustomer(requestVo);
            outBean = new BaseOutBean(ErrorType.NONE, requestVo.getSerialNum());
        } catch (Exception e) {
            logger.info(e.getMessage());
            if (e instanceof UserException) {
                outBean = new BaseOutBean(ErrorType.FAIL_BUSSINESS, requestVo.getSerialNum());
            } else {
                outBean = new BaseOutBean(ErrorType.ERROR_SYS, requestVo.getSerialNum());
            }
            // 日志:异常
            microServiceLogger.doErrorLogger(logger, outBean, procName, e.getMessage(), e);
        }
        // 日志:返回
        microServiceLogger.doOutLogger(logger, outBean, procName);
        return outBean;
    }
}
